<template>
  <el-form :model="emailForm" :rules="email" label-position="top">
    <el-form-item :label="$t('发件邮箱账号')" prop="email_send_account">
      <el-input v-model="emailForm.email_send_account" />
    </el-form-item>
    <el-form-item :label="$t('邮箱密码')" prop="email_password">
      <el-input v-model="emailForm.email_password" type="password" />
    </el-form-item>
    <el-form-item :label="$t('邮箱类型')" prop="email_type">
      <el-select v-model="emailForm.email_type">
        <el-option
          v-for="item in emailType"
          :key="item.id"
          :value="item.value"
          :label="item.form_title"
        />
      </el-select>
    </el-form-item>
    <!-- <h3 class="title" style="margin-top:20px"><span /> <b>收件箱设置</b> </h3> -->
    <el-form-item :label="$t('收件邮箱账号')" prop="email_get_account">
      <el-input v-model="emailForm.email_get_account" />
    </el-form-item>
    <el-button type="primary" class="save" @click="saveEmail">
      {{ $t('保存') }}
    </el-button>
    <el-button type="primary" class="save blue" @click="clearEmail">
      {{ $t('重置') }}
    </el-button>
  </el-form>
</template>

<script>
import systemApi from '@/api/system/system'
export default {
  props: ['data'],
  data() {
    return {
      emailForm: {
        email_get_account: '',
        email_send_account: '',
        email_password: '',
        email_type: '',
        group: this.data.group
      },
      email: {
        email_send_account: [
          {
            required: 'true',
            message: this.$t('请输入发件箱账号'),
            trigger: 'blur'
          }
        ],
        email_password: [
          {
            required: 'true',
            message: this.$t('请输入发件箱密码'),
            trigger: 'blur'
          }
        ],
        email_type: [
          {
            required: 'true',
            message: this.$t('请输入发件邮箱类型'),
            trigger: 'blur'
          }
        ],
        email_get_account: [
          {
            required: 'true',
            message: this.$t('请输入收件箱账号'),
            trigger: 'blur'
          }
        ]
      },
      emailData: [],
      emailType: []
    }
  },
  created() {
    this.emailData = this.data.children
    this.emailType = this.data.children[3].children
    this.emailData.forEach(el => {
      this.emailForm[el.title] = el.value
    })
  },
  methods: {
    saveEmail() {
      systemApi.sysUpdate({ ...this.emailForm }).then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
        }
      })
    },
    clearEmail() {
      systemApi.emailClear().then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
          this.emailForm = {
            email_get_account: '',
            email_send_account: '',
            email_password: '',
            email_type: '',
            group: this.data.group
          }
        }
      })
    }
  }
}
</script>

<style scoped></style>
